from system import AliBaoBaoSystem


def run_demo():
    """
    Ali Bao Bao电商系统演示程序
    """
    print("=" * 60)
    print("欢迎使用 Ali Bao Bao 电商系统演示")
    print("=" * 60)
    
    # 初始化系统
    system = AliBaoBaoSystem()
    print("系统初始化完成！")
    
    # 1. 测试用户注册
    print("\n1. 测试用户注册功能")
    print("-" * 40)
    
    # 注册顾客
    customer, msg = system.register_user(
        user_type="Customer",
        username="customer1",
        password="customer123",
        email="customer1@example.com",
        phone="13800138001",
        address=["北京市朝阳区"]
    )
    print(f"顾客注册: {msg}")
    
    # 注册卖家
    seller, msg = system.register_user(
        user_type="Seller",
        username="seller1",
        password="seller123",
        email="seller1@example.com",
        phone="13800138002",
        shop_name="精品电器店",
        description="销售各类电子产品"
    )
    print(f"卖家注册: {msg}")
    
    # 2. 测试用户登录
    print("\n2. 测试用户登录功能")
    print("-" * 40)
    
    user, msg = system.login("customer1", "customer123")
    print(f"顾客登录: {msg}")
    
    user, msg = system.login("admin", "admin123")
    print(f"管理员登录: {msg}")
    
    # 3. 测试产品管理（卖家功能）
    print("\n3. 测试产品管理功能")
    print("-" * 40)
    
    # 添加电子产品
    electronics, msg = system.add_product(
        seller_id=seller.user_id,
        category="Electronics",
        name="智能手机X99",
        price=2999.00,
        description="最新款智能手机，6.5英寸屏幕，128GB存储",
        stock=100,
        brand="TechBrand",
        warranty=24,
        specifications={"屏幕": "6.5英寸", "存储": "128GB", "摄像头": "4800万像素"}
    )
    print(f"添加电子产品: {msg}")
    
    # 添加服装产品
    clothing, msg = system.add_product(
        seller_id=seller.user_id,
        category="Clothing",
        name="休闲牛仔裤",
        price=199.00,
        description="舒适透气的牛仔裤，多色可选",
        stock=50,
        size="M",
        color="蓝色",
        material="牛仔布"
    )
    print(f"添加服装产品: {msg}")
    
    # 4. 测试产品搜索
    print("\n4. 测试产品搜索功能")
    print("-" * 40)
    
    results = system.search_products("手机")
    print(f"搜索'手机'结果: {len(results)} 个产品")
    for p in results:
        print(f"  - {p.name} (¥{p.price})")
    
    results = system.get_products_by_category("Clothing")
    print(f"服装类别产品: {len(results)} 个产品")
    for p in results:
        print(f"  - {p.name} (¥{p.price})")
    
    # 5. 测试购物车功能
    print("\n5. 测试购物车功能")
    print("-" * 40)
    
    success, msg = system.add_to_cart(customer.user_id, electronics.product_id, 2)
    print(f"添加到购物车: {msg}")
    
    success, msg = system.add_to_cart(customer.user_id, clothing.product_id, 1)
    print(f"添加到购物车: {msg}")
    
    # 查看购物车
    customer_obj = system.get_user_by_id(customer.user_id)
    print(f"购物车商品数: {len(customer_obj.shopping_cart)}")
    for item in customer_obj.shopping_cart:
        print(f"  - {item['product'].name} x {item['quantity']}")
    
    # 6. 测试订单创建
    print("\n6. 测试订单创建功能")
    print("-" * 40)
    
    # 准备订单数据
    items_data = [
        {"product_id": electronics.product_id, "quantity": 2},
        {"product_id": clothing.product_id, "quantity": 1}
    ]
    
    shipping_address = {
        "province": "北京市",
        "city": "北京市",
        "district": "朝阳区",
        "detail_address": "建国路88号",
        "zip_code": "100022",
        "contact_name": "张三",
        "contact_phone": "13800138001"
    }
    
    # 创建订单
    order, msg = system.create_order(
        customer.user_id,
        items_data,
        shipping_address,
        "支付宝"
    )
    print(f"创建订单: {msg}")
    if order:
        print(f"订单ID: {order.order_id}")
        print(f"订单总金额: ¥{order.calculate_total():.2f}")
        print(f"订单状态: {order.status}")
    
    # 7. 测试订单管理（卖家功能）
    print("\n7. 测试订单管理功能")
    print("-" * 40)
    
    # 更新订单状态
    if order:
        success, msg = system.update_order_status(
            order.order_id,
            "Processing",
            seller.user_id
        )
        print(f"更新订单状态: {msg}")
        
        # 设置物流单号
        success, msg = system.set_order_tracking(
            order.order_id,
            "SF1234567890",
            seller.user_id
        )
        print(f"设置物流单号: {msg}")
    
    # 查看卖家的订单
    seller_orders = system.get_seller_orders(seller.user_id)
    print(f"卖家订单数: {len(seller_orders)}")
    
    # 8. 测试评论功能
    print("\n8. 测试评论功能")
    print("-" * 40)
    
    # 添加评论
    review, msg = system.add_review(
        customer.user_id,
        electronics.product_id,
        5,
        "产品非常好，性价比高，物流也很快！"
    )
    print(f"添加评论: {msg}")
    
    # 卖家回复评论
    if review:
        success, msg = system.reply_to_review(
            review.review_id,
            seller.user_id,
            "Seller",
            "感谢您的好评，我们会继续努力提供更好的产品和服务！"
        )
        print(f"回复评论: {msg}")
    
    # 查看产品评论
    reviews = system.get_product_reviews(electronics.product_id)
    print(f"产品评论数: {len(reviews)}")
    for r in reviews:
        print(f"  - {r.comment} (评分: {r.rating}分)")
    
    # 9. 测试管理员功能
    print("\n9. 测试管理员功能")
    print("-" * 40)
    
    # 获取管理员
    admin, _ = system.login("admin", "admin123")
    
    # 管理员查看所有用户
    all_users, _ = system.admin_get_all_users(admin.user_id)
    print(f"系统用户总数: {len(all_users)}")
    
    # 管理员查看所有订单
    all_orders, _ = system.admin_get_all_orders(admin.user_id)
    print(f"系统订单总数: {len(all_orders)}")
    
    # 管理员查看所有产品
    all_products, _ = system.admin_get_all_products(admin.user_id)
    print(f"系统产品总数: {len(all_products)}")
    
    print("\n" + "=" * 60)
    print("演示完成！感谢使用 Ali Bao Bao 电商系统")
    print("=" * 60)


if __name__ == "__main__":
    run_demo()